package javapop.framework.parser.expr.generic;

import javapop.framework.generic.GExprOperator;
import javapop.framework.generic.GExprOperator.Type;
import javapop.framework.parser.expr.ExprAssoc;
import javapop.framework.parser.expr.ExprNode;
import javapop.framework.parser.expr.InfixNode;

public class CInfixNode<T> extends CExprNode<T> implements InfixNode {

	private GExprOperator op;
	private ExprNode left;
	private ExprNode right;
	private int prio;
	private ExprAssoc assoc;
	
	public CInfixNode(GExprOperator op,T data) {
		super(data);
		this.op = op;
		this.prio = op.getPrio();
		this.assoc = convAssoc();
		assert(op.getType() == Type.INFIX);
	}

	@Override
	public int getPrio() {
		return prio;
	}

	/**
	 * La prio de base doit etre fixe et sinon le compilo semble modifier la prio de l'operateur
	 */
	@Override
	public void setPrio(int prio) {
		this.prio = prio;
	}

	@Override
	public ExprNode getLeftOperand() {
		return left;
	}

	@Override
	public void setLeftOperand(ExprNode operand) {
		left = operand;		
	}

	@Override
	public ExprNode getRightOperand() {
		return right;
	}

	@Override
	public void setRightOperand(ExprNode operand) {
		right = operand;
	}

	@Override
	public ExprAssoc getAssoc() {
		return assoc;
	}

	private ExprAssoc convAssoc() {
		switch(op.getAssoc()) {
		case LEFT:
			return ExprAssoc.LEFT;
		case RIGHT:
			return ExprAssoc.RIGHT;
		default:
			return ExprAssoc.NO;
		}
	}

	@Override
	public void setAssoc(ExprAssoc assoc) {
		this.assoc = assoc; 		
	}

	@Override
	public boolean isInfix() {
		return true;
	}
}
